﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using Qplus.enums;
using Qplus.protocol;
using System.Diagnostics;
using System.Collections.ObjectModel;

namespace Qplus.Infrastructure
{
    public class PipManager
    {
    ObservableCollection<IMessageID> list;

	private static PipManager instance;
	
	private PipManager() {
        list = new ObservableCollection<IMessageID>();
	}

	public static PipManager getInstance() {
		if (instance == null) {
			instance = new PipManager();
		}
		return instance;
	}
	
	public void release(){
		list.Clear();
		instance = null;
	}

	/**
	 * 找到对应的包
	 * @param id
	 * @return
	 */
	public InPacket getPacket(int id){
		if(list == null){
			return null;
		}
        //TODO
		//IMessageID msgID = null;
        if (Enum.IsDefined(typeof(MMessageID), id))
        {
            MessageID.getInstance().setBody((MMessageID)id);
            return (InPacket)MessageID.getInstance().getBody();
        }
		Debug.WriteLine("找不到对应的id");
		return null;
	}
	
	public void registerMessageID(IMessageID messageID){
		if(list.Contains(messageID)){
			return;
		}
		
		list.Add(messageID);
	}
	
	public  static void regBuffer(IMessageID messageID) {
		PipManager m = PipManager.getInstance();
		m.registerMessageID(messageID);
	}
    }
}
